<template>
	<view class="outBox">
		<u-navbar leftText="个人信息维护" leftIconColor="#fff" :autoBack="true" bgColor="#242331">
			<view class="left-slot" slot="left">
				 <u-icon name="arrow-left" color="#fff"></u-icon>
				<text>个人信息维护</text>
			</view>
		</u-navbar>
		<view class="content" :style="{
			marginTop:menuHeight + 'px',
		}">
			<image class="bg" :src="baseImgUrl + 'reservation/banner.png'" mode="scaleToFill"></image>
			<view class="itemInner">
				<view class="item">
					<view class="name">
						头像
					</view>
					<view class="value">
						<image class="headImg" :src="userInfo.avatar" mode="widthFix"></image>
					</view>
				</view>
				<view class="item">
					<view class="name">
						性别
					</view>
					<view class="value" @click="handleShowGender">
						<text>{{userInfo.sex == '' ? '请选择' : userInfo.sex}}</text>
						<view class="icon">
							<u-icon name="arrow-right" color="#fff" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="item">
					<view class="name">
						昵称
					</view>
					<view class="value">
						<text v-if="!isEdit">{{userInfo.nickName}}</text>
						<u--input v-else v-model="userInfo.nickName" color="#fff" inputAlign="right" placeholder="请输入昵称"
							border="bottom" clearable></u--input>
						<view class="icon">
							<u-icon name="arrow-right" color="#fff" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="item">
					<view class="name">
						实名认证
					</view>
					<view class="value">
						<text>{{userInfo.isCheckRealName == 0 ? '未完成' : '已完成'}}</text>
						<view class="icon">
							<u-icon name="arrow-right" color="#fff" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="item">
					<view class="name">
						通知手机号
					</view>
					<view class="value">
						<text>{{userInfo.mobile}}</text>
						<view class="smallBtn" @click="showOnbind = true" v-if="userInfo.mobile">
							<text>解绑</text>
						</view>
						<view class="smallBtn" v-else @click="handleBind">
							<text>绑定</text>
						</view>
						<view class="icon">
							<u-icon name="arrow-right" color="#fff" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="qrCode" v-if="userInfo.isCheckRealName == 1">
					<image src="../../static/mine/qrCode.png" mode="widthFix"></image>
				</view>
				<view class="btns">
					<view class="btn" v-if="!isEdit" @click="isEdit = true">编辑</view>
					<view class="btn" v-else @click="handleSave">保存</view>
				</view>
			</view>
			<u-picker :show="showGender" :defaultIndex="defaultIndex" :columns="genderList" @confirm="confirmGender"
				@cancel="showGender = false"></u-picker>

			<u-modal :show="showOnbind" title="解绑" content='是否继续解绑该手机号' @confirm="confirmOnbind"
				:showCancelButton="true" @cancel="showOnbind = false"></u-modal>

			<bindPhone ref="bindPhone" :mobile="phone" @handleSuc="getUserInfo()" />
		</view>
	</view>
</template>

<script>
	import {
		getWXStatusHeight
	} from '@/utils/index.js'

	import bindPhone from "@/components/bindPhone/index"

	import {
		findCustomerInfoApi,
		updateUserInfo,
		unbindMobileApi,
		bindPhoneApi,
		getCodeApi
	} from '../../api/api';

	export default {
		components: {
			bindPhone,
		},
		data() {
			return {
				seconds: 90,
				// 胶囊的宽度
				barWidth: 0,
				menuHeight: 0,
				userInfo: {
					avatar: '',
					sex: '',
					nickName: '',
					isCheckRealName: 1, // 是否实名认证
					mobile: ''
				},
				defaultIndex: [0], // 性别默认值
				showGender: false, // 显示性别
				genderList: [
					['男', '女']
				],
				baseImgUrl: this.$baseImgUrl,
				isEdit: false, // 是否是编辑
				showCode: false,
				// 解绑
				showOnbind: false,
				phone: '',
			}
		},
		onLoad() {
			let obj = getWXStatusHeight()
			this.barWidth = obj.barWidth + 3
			this.menuHeight = obj.menuHeight

			this.getUserInfo()
		},
		methods: {
			// 绑定
			handleBind() {
				this.phone = this.userInfo.mobile
				this.$refs.bindPhone.showCode = true
			},
			// 继续解绑
			async confirmOnbind() {
				const res = await unbindMobileApi()
				if (res.code == 200) {
					this.showOnbind = false
					this.userInfo.mobile = ""
					this.$refs.bindPhone.showCode = true
				}
			},
			// 获取用户信息
			async getUserInfo() {
				const res = await findCustomerInfoApi()
				if (res.code == 200) {
					this.userInfo = res.data
					if (res.data.sex == "女") this.defaultIndex = [1]
				} else {
					console.log("1111");
					this.$refs.bindPhone.showCode = true
				}
			},
			// 保存
			async handleSave() {
				uni.showToast({
					title: '保存成功',
					duration: 1000
				});

				const {
					avatar,
					...otherData
				} = this.userInfo
				const res = await updateUserInfo(otherData)
				if (res.code == 200) {
					this.isEdit = false
					this.getUserInfo()
				}
			},
			handleShowGender() {
				if (this.isEdit) {
					this.showGender = true
				}
			},
			// 确认选择性别
			confirmGender(e) {
				this.userInfo.sex = e.value[0]
				this.showGender = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.smallBtn {
		width: 90rpx;
		padding: 7rpx 0;
		text-align: center;
		font-size: 28rpx;
		color: #fff;
		background-color: #5aa10c;
		border-radius: 15rpx;
		margin-left: 10rpx;
	}

	.qrCode {
		margin: 50rpx auto;
		display: flex;
		justify-content: center;
		align-items: center;

		image {
			width: 250rpx;
		}
	}

	.btns {
		display: flex;
		justify-content: center;
		margin-top: 50rpx;

		.btn {
			padding: 20rpx 40rpx;
			color: #fff;
			border-radius: 20rpx;
			background-color: #5aa10c;
		}
	}

	.headImg {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
	}

	.itemInner {
		position: relative;
		z-index: 99;

		.icon {
			width: 30rpx;
			height: 30rpx;
			background-color: #272532;
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-left: 10rpx;
		}

		.item {
			position: relative;
			z-index: 3;
			padding: 15px 30rpx;
			border-bottom: 1rpx solid #454b56;

			.name {
				display: flex;
				align-items: center;
			}
		}


		.value {
			color: #fff;
			text-align: right;
			display: flex;
			align-items: center;
		}

		.item {
			display: flex;
			justify-content: space-between;
			color: #fff;
		}
	}

	.bg {
		width: calc(100% - 80rpx);
		height: 100%;
		position: absolute;
		z-index: 2;
		border-radius: 20rpx;
		overflow: hidden;
	}

	.content {
		position: relative;
		padding: 20rpx 40rpx;
		box-sizing: border-box;
		min-height: calc(100vh - 40rpx);
	}

	.left-slot {
		color: #fff;

		image {
			width: 40rpx;
			margin-right: 30rpx;
		}
	}

	.outBox {
		background-color: #242331;
	}
</style>
